package howe.sudoku.entity;

import android.os.Bundle;
import android.os.SystemClock;

public class SudokuGame {

    public static final int GAME_STATE_PLAYING = 0;
    public static final int GAME_STATE_NOT_STARTED = 1;
    public static final int GAME_STATE_COMPLETED = 2;

    private long mId;
    private long mCreated;
    private int mState;
    private long mTime;
    private long mLastPlayed;
    private String mNote;

    // Time when current activity has become active.
    private long mActiveFromTime = -1;

    public static SudokuGame createEmptyGame() {
        SudokuGame game = new SudokuGame();
        // set creation time
        game.setCreated(System.currentTimeMillis());
        return game;
    }

    public SudokuGame() {
        mTime = 0;
        mLastPlayed = 0;
        mCreated = 0;

        mState = GAME_STATE_NOT_STARTED;
    }

    public void saveState(Bundle outState) {
        outState.putLong("id", mId);
        outState.putString("note", mNote);
        outState.putLong("created", mCreated);
        outState.putInt("state", mState);
        outState.putLong("time", mTime);
        outState.putLong("lastPlayed", mLastPlayed);
    }

    public void restoreState(Bundle inState) {
        mId = inState.getLong("id");
        mNote = inState.getString("note");
        mCreated = inState.getLong("created");
        mState = inState.getInt("state");
        mTime = inState.getLong("time");
        mLastPlayed = inState.getLong("lastPlayed");
    }

    public void setNote(String note) {
        mNote = note;
    }

    public String getNote() {
        return mNote;
    }

    public void setCreated(long created) {
        mCreated = created;
    }

    public long getCreated() {
        return mCreated;
    }

    public void setState(int state) {
        mState = state;
    }

    public int getState() {
        return mState;
    }

    /**
     * Sets time of play in milliseconds.
     * @param time
     */
    public void setTime(long time) {
        mTime = time;
    }

    /**
     * Gets time of game-play in milliseconds.
     * @return
     */
    public long getTime() {
        if (mActiveFromTime != -1) {
            return mTime + SystemClock.uptimeMillis() - mActiveFromTime;
        } else {
            return mTime;
        }
    }

    public void setLastPlayed(long lastPlayed) {
        mLastPlayed = lastPlayed;
    }

    public long getLastPlayed() {
        return mLastPlayed;
    }

    public void setId(long id) {
        mId = id;
    }

    public long getId() {
        return mId;
    }

    /**
     * Start game-play.
     */
    public void start() {
        mState = GAME_STATE_PLAYING;
        resume();
    }

    public void resume() {
        // reset time we have spent playing so far, so time when activity was not active
        // will not be part of the game play time
        mActiveFromTime = SystemClock.uptimeMillis();
    }

    /**
     * Pauses game-play (for example if activity pauses).
     */
    public void pause() {
        // save time we have spent playing so far - it will be reseted after resuming
        mTime += SystemClock.uptimeMillis() - mActiveFromTime;
        mActiveFromTime = -1;

        setLastPlayed(System.currentTimeMillis());
    }

    /**
     * Resets game.
     */
    public void reset() {
        setTime(0);
        setLastPlayed(0);
        mState = GAME_STATE_NOT_STARTED;
    }

    public interface OnPuzzleSolvedListener
    {
        /**
         * Occurs when puzzle is solved.
         *
         * @return
         */
        void onPuzzleSolved();
    }
}